<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>购物车</title>
		<meta name="description" content="安装通（azt365.com）中国大型的b2b电子商务智慧化平台,整合了国内安装行业内采购商与供应商,打通上下游产业链,信息透明共享,通过云平台,大数据,为各大企业供应商提供产品信息、企业推广,为您提供全面的安装行业资讯金融服务以及线上支付.">
		<meta name="keywords" content="安装通,azt365,江苏b2b电子商务,江苏建筑采供批发平台,江苏弱电批发,江苏消防报警系统,江苏智慧安装建筑安装,江苏安防监控系统,江苏通风设备安装,江苏通风工程安装,江苏通风工程施工,江苏消防工程材料批发,江苏消防工程材料">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link href="/res/css/mall/mallIndex.css" rel="stylesheet">
		<link href="/res/css/mall/shoping.css" rel="stylesheet">
        <style type="text/css">
            .ms-controller{
                visibility: hidden
            }
		</style>
	</head>
<body>
[#include "/front/common/front_headMal.html"]  
<div class="CommodityBox ms-controller" :controller="app" style="margin: 100px auto 10px;margin-top: 10px;">

    <div class="empCar" :visible="!hasCart" style="margin: 100px 0;">
        <img alt="noCart" src="/res/images/godCar/empCar_03.png"/>
        <h5>购物车空空的哦，快去挑选合适的商品吧~
		<a href="/member">去购物></a></h5>
    </div>

	<div class="CommodityCate" :visible="@hasCart">
		<span class="th-chk" style="position: relative;">
			<span class="checkspan" :class="@allChecked ? 'on' : 'off'"></span>
			<input class="checkalljq" type="checkbox" :click="@checkAll" :attr="{checked : @allChecked}" >全选
		</span>
		<span class="th-item">商品信息</span>
		<span class="th-price">单价</span>
		<span class="th-amount">数量</span>
		<span class="th-sum">小计</span>
		<span class="th-op">操作</span>
	</div> 
	<div class="CommodityList" :visible="@hasCart">

		<div :for="(data_index, data) in @list">
			<div class="AProduct areacolor cf" style="border-left: none;border-right: none;height: 40px;">
				<div class="category th-item" style="position: relative;">
					<span class="checkspan" style="cursor: pointer;" :class="@data.checked ? 'on' : 'off'" :visible="@data.canBuy"></span>
					<input class="checkalljq" type="checkbox" style="position: relative;left: -10px;" :click="@checkCompany(data_index, $event)" :attr="{checked : @data.checked}" >{{@data.companyName}}
					<span style="margin-left: 5px; position: relative; top: 4px;">
						<a style="cursor: default; visibility: hidden;" :if="data.qq == null || data.qq == ''">
							<img alt="" src="/res/images/godCar/callme_03.png" >
						</a>
						<a :attr="{href : 'tencent://message/?uin=' + @data.qq + '&amp;Site=www.azt365.com&amp;Menu=yes'}" :if="data.qq != null && data.qq != ''">
							<img alt="" src="/res/images/godCar/callme_03.png">
						</a>
					</span>
				</div>
			</div>

			<div :for="(pro_index, pro) in @data.productList">
				<div class="AProduct areacolor cf" :class="{disab : !pro.canBuy}">
                    <div class="category th-chk" style="position: relative;" :if="pro.canBuy">
                        <span class="checkspan" style="left:18px;top:19px;" :class="@pro.checked ? 'on' : 'off'"></span>
                        <input class="checkalljq" type="checkbox" :click="@checkProduct(data_index, pro_index, $event)" :attr="{checked : @pro.checked}" style="margin:20px 0px 0px 20px;">
                    </div>
					<div class="category th-chk" style="position: relative;" :if="!pro.canBuy">
						<span class="checkspan off" style="left:18px;top:19px;">失效</span>
					</div>

					<div class="category th-item">
						<div class="property cf">
							<div class="pro-img">
								<a target="_blank" :attr="{href : '/offer/' + pro.productId + '.html'}">
									<img :attr="{src: @pro.picPath}"> </a>
							</div>
							<p class="pro-title">
								<a target="_blank" :attr="{title : @pro.productName, href : '/offer/' + pro.productId + '.html'}">{{@pro.productName | truncate(25, '...')}}</a>
								<p style="float: left;color: #999;">{{@pro.skuStr}}</p>
							</p>
							<div class="pro-pro">
								<p class="pro-check"></p>
							</div>
						</div>
					</div>
					<div class="category th-price">
						<div class="pro-price" style="margin-top: 12px;">
							￥<em>{{@pro.realPrice | toFixed}}</em>
						</div>
					</div>

                    <div class="category th-amount" :if="pro.canBuy">
                        <div class="pro-number">
                            <a href="javascript:void(0)" title="减少" class="nums-red" :click="@subQuantity(pro)">-</a>
                            <input type="text" class="pro-nums" :duplex-number="@pro.quantity" :input="fix(pro, $event)" :blur="fix2(pro, $event)">
                            <a href="javascript:void(0)" title="增加" class="nums-add" :click="@addQuantity(pro)">+</a>
                        </div>
                    </div>
					<div class="category th-amount" :if="!pro.canBuy">
						{{@pro.quantity}}
					</div>

					<div class="category th-sum" style="margin-top: 23px;color: #FFB90F;font-size: 18px;font-weight: bold;">
						<span class="subtotal">￥{{@pro.realPrice * pro.quantity | toFixed}}</span>
					</div>
					<div class="category th-op" style="margin-top: 23px;">
						<div class="decidetime">
							<a class="pro-remove" href="javascript:void(0);" :click="@removePro(data_index, pro_index)">删除</a>
						</div>
					</div>
				</div>
			</div>
        </div>

	</div>
	<div class="PayofTable" :visible="@hasCart">
		<div class="PayofTwo cf">
			<div class="PayoUnderleft" style="float: left;padding-top: 18px;position: relative;">
				<span class="checkspan" style="top:22px;left:2px;" :class="@allChecked ? 'on' : 'off'"></span>
				<input class="checkalljq" type="checkbox" style="margin-right: 5px;" :click="@checkAll" :attr="{checked : @allChecked}" data-check="all">全选
				<span style="margin-left: 5px;"><a href="javascript:void(0)" :click="@removeChecked">删除选中的商品</a></span>
			</div>
			<button class="paybtn" type="button" :click="@submitOrder">去结算</button>
			<div class="TotalAmount" style="padding-top: 18px;">
				<span>已选择<strong class="total"><span id="totalcase">{{@global.checkedNum}}</span></strong>件商品</span>
				<span>&nbsp;总计：<strong class="total">￥<span id="totalamount">{{@global.totalPrice | toFixed}}</span></strong>（不含运费）</span>
			</div>
		</div>
	</div>
</div>

<script type="text/html" id="noCheckWin">
    <div class="carWarn">
        <h5>
            <img alt="" src="/res/images/oparaStatus/emailWarn.jpg"/>
            没有选择任何商品
        </h5>
        <button type="button">确定</button>
    </div>
</script>

[#include "/front/common/front_foot.html"]
<script type="text/javascript" src="/res/js/common/avalon.js"></script>
<script type="text/javascript" src="/res/js/common/js.cookie.js"></script>
<script type="text/javascript" src="/res/js/common/util.js"></script>
<script type="text/javascript" src="/res/js/common/common.js"></script>
<script type="text/javascript" src="/res/third/layer/layer.js"></script>
<script type="text/javascript">
    avalon.filters.toFixed = function(val) {
		return val.toFixed(2);
	}

    var vm = avalon.define({
        $id: "app",
        list : [],       	//购物车数据
        allChecked : false,
        hasCart : false,
		$computed : {
			//全局对象（包含一些全局属性）
			global : function() {
				var checkedNum = 0;		//勾选的商品数量
				var totalPrice = 0;		//勾选的商品总价
				for(var i = 0; i < vm.list.length; i++) {
					for(var j = 0; j < vm.list[i].productList.length; j++) {
						var pro = vm.list[i].productList[j];
						if(pro.canBuy && pro.checked) {
							checkedNum ++;
							totalPrice += pro.quantity * pro.realPrice;
						}
					}
				}
				return {checkedNum : checkedNum, totalPrice : totalPrice};
			}
		},
		//只能输入数字
        fix: function(pro, $event) {
            $event.target.value = $event.target.value.replace(/\D+/, '');
			if($event.target.value == '') {
                $event.target.value = 1;
			}
        },
		fix2 : function(pro, $event) {
            var value = Number($event.target.value.replace(/\D+/, ''));
            if(!(value && value >= 1 && value <= pro.stock)) {
                //还原数量
				var array = getCart();
				var quantity = 1;
				$.each(array, function() {
					if(this.skuId == pro.skuId) {
						quantity = this.quantity;
						return false;
					}
				});
				value = quantity;
            } else {
				//保存
                removeCart(pro.skuId);
				addCart(pro.skuId, value);
			}
			pro.quantity = Number(value);
		},
        //加
        addQuantity : function(pro) {
			if(pro.quantity + 1 <= pro.stock) {
                pro.quantity++;
                addCart(pro.skuId, 1);
            }
        },
        //减
        subQuantity : function(pro) {
            if(pro.quantity > 1) {
                pro.quantity --;
                addCart(pro.skuId, -1);
            }
        },
		checkAll : function(e) {
            var checked = $(e.target).prop('checked');
			this.allChecked = checked;
            $.each(vm.list, function() {
				if(this.canBuy) {
                    this.checked = checked;
                }
                $.each(this.productList, function() {
					if(this.canBuy) {
                        this.checked = checked;
                    }
                });
            });
		},
		checkCompany : function(index, $event) {
			var checked = $($event.target).prop('checked');
			this.list[index].checked = checked;
			$.each(this.list[index].productList, function() {
				this.checked = checked;
            });
            commonCheck();
		},
		checkProduct : function(cindex, pindex, $event) {
			var checked = $($event.target).prop('checked');
			this.list[cindex].productList[pindex].checked = checked;
            commonCheck();
		},
		//删除商品
		removePro : function(cindex, pindex) {
			var pro = this.list[cindex].productList[pindex];
			//从cookie中删除
            removeCart(pro.skuId);
            this.list[cindex].productList.removeAt(pindex);
			if(this.list[cindex].productList.length == 0) {
				this.list.removeAt(cindex);
			}
			if(this.list.length == 0) {
				this.hasCart = false;
			}
		},
		//删除选中
        removeChecked : function() {
            var array = [];
			var cindex = 0;
            $.each(this.list, function() {
				var pindex = 0;
                $.each(this.productList, function() {
                    if(this.checked) {
                        array.push({cindex : cindex, pindex: pindex});
                    }
                });
            });
			if(array.length == 0) {
				layer.alert('请选择需要删除的商品');
				return;
			}
			layer.confirm("确定要从购物车中删除这些商品吗？", function(index) {
                $.each(array, function() {
                    vm.removePro(this.cindex, this.pindex);
                });
                layer.close(index);
			});
		},
		//下单
        submitOrder : function() {
			var array = [];
			$.each(this.list, function() {
				$.each(this.productList, function() {
					if(this.checked && this.canBuy) {
						array.push({skuId : this.skuId, quantity : this.quantity});
					}
				});
			});
			if(array.length == 0) {
				layer.alert('没有选择任何商品');
				return;
			}
            $.my.postForm("/order/submit/product",{data: JSON.stringify(array)});
		}
    });

	function commonCheck() {
        var canBuyNumAll = 0;
        var checkedAll = 0;
        $.each(vm.list, function() {
            var canBuyNumInCompany = 0;
            var checkedInCompany = 0;
            $.each(this.productList, function() {
                if(this.canBuy)  {
                    canBuyNumInCompany ++;
                    canBuyNumAll ++;
                    if(this.checked) {
                        checkedInCompany ++;
                        checkedAll ++;
                    }
                }
            });
            this.checked = canBuyNumInCompany == checkedInCompany;
        });
        vm.allChecked = canBuyNumAll == checkedAll;
	}

    avalon.ready(function() {
		layer.ready(function() {
			$.ajax({
				url : '/listCart',
				type : 'post',
				dataType : 'json',
				beforeSend : function() {
					layer.load(2, {shade: 0.1});
				},
				success : function(res) {
					if(res && res.success) {
						var list = res.obj;
						//先初始化checked属性
                        $.each(list, function() {
                            this.checked = true;
                            $.each(this.productList, function() {
                                this.checked = true;
                            });
                        });
						$('input[type=checkbox][data-check=all]').click();
						vm.list = list;
						if(list.length > 0) {
							vm.hasCart = true;
						}
					} else {
					}
				},
				complete : function() {
					layer.closeAll('loading');
				}
			});
        });
    });

</script>
</body>
</html>